package tts.dao;
// default package
// Generated 16-dic-2013 11.40.27 by Hibernate Tools 4.0.0

import java.util.Collection;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import tts.model.Roles;
import tts.model.Users;

/**
 * Home object for domain model class Roles.
 * @see .Roles
 * @author Hibernate Tools
 */

@Repository("rolesHome")
@Transactional(propagation = Propagation.REQUIRED)
public class RolesHome {

	private static final Log log = LogFactory.getLog(RolesHome.class);

	@PersistenceContext
	private EntityManager entityManager;

	public void insert(Roles transientInstance) {
		log.debug("persisting Roles instance");
		try {
			entityManager.persist(transientInstance);
			log.debug("persist successful");
		} catch (RuntimeException re) {
			log.error("persist failed", re);
			throw re;
		}
	}

	public Roles update(String roleName, Roles newRole) throws RuntimeException {
		log.info("updating Roles instance: " + roleName);
		try {
			Roles result = this.findByRoleName(roleName);
			result.setRoleName(newRole.getRoleName());
			result.setNotes(newRole.getNotes());
			result.getUsersList().clear();
			for (Users u : newRole.getUsersList()) {
				result.getUsersList().add(u);
			}
			log.info("update successful");
			return result;
		} catch (RuntimeException re) {
			log.error("update failed", re);
			throw re;
		}
	}

	public void remove(Roles persistentInstance) {
		log.debug("removing Roles instance");
		try {
			entityManager.remove(persistentInstance);
			log.debug("remove successful");
		} catch (RuntimeException re) {
			log.error("remove failed", re);
			throw re;
		}
	}

	public Roles merge(Roles detachedInstance) {
		log.debug("merging Roles instance");
		try {
			Roles result = entityManager.merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public Roles findById(Integer id) {
		log.debug("getting Roles instance with id: " + id);
		try {
			Roles instance = entityManager.find(Roles.class, id);
			log.debug("get successful");
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public Roles getUserRole() {
		log.debug("getting Role instance with role= ROLE_USER");
		try {
			Roles instance = (Roles) entityManager.createQuery("from Roles where roleName='ROLE_USER'").getResultList().get(0);
			log.debug("get successful");
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public Roles findByRoleName(String roleName) {
		log.debug("getting Roles instance "+ roleName);
		try {
			Roles instance = (Roles) entityManager.createQuery("from Roles where roleName='"+ roleName +"'").getResultList().get(0);
			log.debug("get successful");
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	@SuppressWarnings("unchecked")
	public Collection<Roles> findAll() throws RuntimeException {
		log.info("getting all Roles in db");
		try {
			Collection<Roles> list = entityManager.createQuery("from Roles order by roleName").getResultList();
			log.info("get successful");
			return list;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

//	@SuppressWarnings("unchecked")
//	public Collection<Roles> findByUser(Users user) {
//		log.debug("getting Roles for user"+ user.getUsername());
//		try {
//			Collection<Roles> rolesList = entityManager.createQuery("from UsersRoles where users_id="+user.getUsers_id()).getResultList();
//			log.debug("get successful");
//			return rolesList;
//		} catch (RuntimeException re) {
//			log.error("get failed", re);
//			throw re;
//		}
//	}
}
